var zb={};//命名空间
    zb.global={
    	version:"3167.001",
    	url:"http://192.168.1.127:908/",
    	id:"",
    	val:"",
    	menu:{},
    	data:{},
    	list_url:""
    }

    zb.submit= function(urlStr,model , action, cmdkey , cmdtxt, session, submitType, ymid){
       showLoader();
       $.ajax({
			 type:"post",
			 url:zb.global.url+""+urlStr,
			 contentType: "application/json",
			 dataType:"json",
			 async:false, 
			 data:CommonConfClass(model , action, cmdkey , cmdtxt, session, submitType),
			 success:function(data){
				 switch(submitType){
				 case "login" :
					   goTo("home.html",false);
					   rememberUserInfo(data);
					   zb.global.menu=data;
					   break;
				  default :
					  zb.global.data=data;
				      addCaption(data);
				      route(ymid);
				 }
			 },
			 complete: function() { 
			     hideLoader();       
	         },
			 error:function (XMLHttpRequest, textStatus, errorThrown){
				    alert(XMLHttpRequest.readyState);  
			 }
    	});	
    }
    
function route(ymid) {  
	switch(ymid){
	    case "home":
	    	loadIndex();
	    	break;
		case "exit" :
			exit();
			break;
		case "module" : 
			routeModule();
			break;
		case "add" : 
			//billAdd();
			break;
		default :

	}
}
 
function CommonConfClass(model , action, cmdkey , cmdtxt , session, submitType){
	var condition=[];
	var request = new RequestSendContent();
	switch (submitType){
		case "login":
			 condition.push(new ReqestSendData("user",stringToHex(des("zbintel4", $("#user").val() , 1, 1, "zbintel4", 1))));
			 condition.push(new ReqestSendData("password",stringToHex(des("zbintel4", $("#password").val() , 1, 1, "zbintel4", 1))));
			 condition.push(new ReqestSendData("serialnum",stringToHex(des("zbintel4", "99000549378372" , 1, 1, "zbintel4", 1))));
			 condition.push(new ReqestSendData("rndcode",stringToHex(des("zbintel4", "" , 1, 1, "zbintel4", 1))));
			 condition.push(new ReqestSendData("version",zb.global.version));	
			 break;
		case "remind":
			condition = setCondion(submitType);
			break;
		case "loadlist":
			condition = setCondion(submitType);
			break;
		case "update":
			condition = setCondion(submitType);
			break;
		default :
			 //condition = setCondion(submitType);
	 } 
     request.model=model;  
     request.action=action;  
     request.cmdkey=cmdkey;  
     request.cmdtxt=cmdtxt;  
     request.session = session;
     request.datas=condition; 
     return JSON.stringify(request);
}
    
function loadHome(str){
	addSelectSign(str);
	zb.submit(zb.global.menu.body.sys.homeurl,'init' , 'sys', '' , '' , zb.global.menu.header.session, 'home','home');
}

//add list head right menu
function addHeadToolbar(menu,Id){
	var el=document.getElementById(Id);
	var ul=document.createElement("ul");
	    ul.className="toolbar_1";
	    for(var i=0;i<menu.tools.length;i++){
		    var li=document.createElement("li");	
		        li.setAttribute("type",menu.tools[i].ico);
		        li.setAttribute("url",menu.tools[i].url);
			    li.appendChild(addHeadMenu(menu.tools[i]));
			    $(li).bind("click tap",function(){			    	
			    	var type=$(this).attr("type");
			    	switch(type){
			    		case "add":
			    			  goTo('add.html',true);
			    			  zb.submit($(this).attr("url"),'bill' , 'init', '' , '' , zb.global.menu.header.session, '','add');
			    			break;
			    		case "adsearch":
			    			 break;
			    	    default:
			    	}
			    	
			    });
		    	ul.appendChild(li);
	    }
	    $( "#popup").popup("close");
	    $(el).find("ul").remove();
	    el.appendChild(ul);
}

function addHeadMenu(menu){
	var div_1=document.createElement("div");
    var div_2=document.createElement("div");
    var p=document.createElement("p");
    switch(menu.ico){
        case "add":
        	div_2.className="icon add";
        	break;
        case "adsearch":
        	div_2.className="icon search";
        	break;
        default:
    
    }
    p.innerHTML=menu.caption;
	div_1.appendChild(div_2);
	div_1.appendChild(p);
	return div_1;
}

//add head list sort
function addHeadSort(menu,Id){
	var el=document.getElementById(Id);
	var ul=document.createElement("ul");
    ul.className="toolbar_2";
    for(var i=0;i<menu.sorts.sortoptions.length;i++){
    	 var li=document.createElement("li");
    	     li.setAttribute("id",menu.sorts.id);
    	     li.setAttribute("val",menu.sorts.sortoptions[i].v);
    	     li.innerHTML=menu.sorts.sortoptions[i].n;
    	     $(li).bind("click tap",function(){
    	    	 $( "#popupsort").popup("close");
    	    	 zb.global.id=$(this).attr("id");
    	    	 zb.global.val=$(this).attr("val"); 
    	    	 refreshList(zb.global.list_url,'','loadlist');
    	     });
    	     ul.appendChild(li);
    	     
    }
    $(el).find("ul").remove();
	el.appendChild(ul);
}

function headTools(menu){
	var htm="";
	if(menu.hasOwnProperty("sorts")){
		 htm+="<a class='line'></a><a class='sortbtn' href='#popupsort'  data-rel='popup'></a>";
		 addHeadSort(menu,'popupsort');
	}
	if(menu.hasOwnProperty("tools")){
		if(menu.tools.length==1){
			 htm+="<a class='line'></a><span class='save' onclick=\"headSave('"+zb.global.data.body.bill.tools[0].url+"','bill' , 'init', '' , '' , '"+zb.global.session+"', 'remind','module')\"></span>";
		}
		else{
			 htm+="<a class='line'></a><a class='menubtn' href='#popup'  data-rel='popup'></a>";
			  addHeadToolbar(menu,'popup');
		}
	}
	$(".tools").empty().append(htm);
}

//退出
function exit(){
    if(zb.global.data.header.status==0){
   	   navigator.app.exitApp();
    }   
}

function addCaption(data){
	if(data.body.hasOwnProperty('bill')){
		$("#addpage [name='caption']").html(data.body.bill.caption);
	}
	else if(data.body.hasOwnProperty('home')){
		$("[name='caption']").html(data.body.home.caption);
	}
	else if(data.body.hasOwnProperty('report')){
		$("[name='caption']").html(data.body.report.caption);
	}	
}

//load 首页工作台
function loadIndex(){
	$("#pages ,#navbar ,#search").empty();
	headTools("");
	var html="";
	for(var i=0;i<zb.global.data.body.home.groups.length;i++){
		   html+="<div id='"+zb.global.data.body.home.groups[i].id+"' class='topic'>"+zb.global.data.body.home.groups[i].caption+"</div>";
	       if(zb.global.data.body.home.groups[i].fields[0].type=="icoview"){
			   html+="<div class='cols'>";
			   for(var j=0;j<zb.global.data.body.home.groups[i].fields[0].source.icoview.length;j++){
				   html+="<div class='item'>" +
				   		"<div class='item-icon'><img src='image/remind_dsfy_press.png' width='60px' height='60px'></div>"+
				        "<div class='item-txt'>"+zb.global.data.body.home.groups[i].fields[0].source.icoview[j].caption+"</div></div>";
			   }
			   html+="</div>";
		   }
	       else{	    	   
	    	   html+="<table class='table tb_1'>";
	    	   var fields = zb.global.data.body.home.groups[i].fields;
	    	   for(var j=0;j<fields.length;j++){
	    		   var rows = fields[j].source.table.rows;
		    	   var cols = fields[j].source.table.cols;
		    	   var lfelds =fields[j].source.table.layout.fields;
	    		   html+="<tr>";
	    		   for(var n=0;n<rows.length;n++){  
	    			   for(var k=0;k<lfelds.length;k++){
	    				   var idx = 0;
	    				   for(var m=0;m<cols.length;m++){
	    					   if (lfelds[k].text == "{@"+cols[m].id+"}"){	idx = m; }
	    				   }
	    				   html+="<td>"+rows[n][idx]+"</td>"; 
	    			   }
	    			   html+="</tr>";  
	    		   }
	    	   }
	    	   html+="</table>";
	       }
	   }
	   $("#module").empty().append(html);
	   $(".tb_1 td:odd").addClass("tx_r");
	   $(".tb_1 td:first").addClass("td_l");
}

function routeModule(){	
    if(zb.global.data.body.model=="bill"){
    	if(zb.global.data.body.bill.uitype == "bill.remind"){
    		headTools(zb.global.data.body.bill);
    		loadRemind();
    	}
    }
    else if(zb.global.data.body.model=="report"){
    	headTools(zb.global.data.body.report);
    	createNavbar();   	
    }	
}


function loadRemind(){
	$("#pages ,#navbar ,#search").empty();
	var htm="<table class='table tb_2' name='remind'>";
	var groups=zb.global.data.body.bill.groups;
    for(var i=0;i<groups.length;i++){	
		 htm+="<tr pos_id='"+groups[i].fields[1].id+"' pos_val='"+groups[i].fields[1].value+"' onclick=setRemindCkb('"+groups[i].fields[2].id+"')  id='"+groups[i].fields[2].id+"' value='"+groups[i].fields[2].value+"'>";
		 htm+="<td>"+groups[i].fields[0].text+"</td>";		 
		 htm+="<td class='td_r'><input type='checkbox'   name='"+groups[i].fields[2].id+"' "+ ((groups[i].fields[2].value=="0") ? "checked='checked'" :"") +"></td>";	 
		 htm+="</tr>";
	}
	htm+="</table>";
	$("#module").empty().append(htm);

}
function setRemindCkb(Id){
	if($("[name='"+Id+"']").is(":checked")){
		$("[name='"+Id+"']").removeAttr("checked");
		$("#"+Id).attr("value","1");
	}else{
		$("[name='"+Id+"']").attr("checked","checked");
		$("#"+Id).attr("value","0");
	}
}

function headSave(urlStr,model , action, cmdkey , cmdtxt, session, submitType, ymid){
	zb.submit(urlStr,model , action, cmdkey , cmdtxt, session, submitType, ymid);
}

//submit condition set
function setCondion(type){
	var array=new Array();
	var condition="";
	switch(type){
		case "remind":
			$.each($("[name='"+type+"'] tr"),function(i,u){
			      condition = new Condition($(u).attr("id"),$(u).attr("value"));
			      array.push(condition);
			      condition = new Condition($(u).attr("pos_id"),i);
			      array.push(condition);
			});	
		break;
		case "loadlist":
			condition = new Condition(zb.global.id,zb.global.val);		
			array.push(condition);
			break;
		case "update":
			array=getCondion(type);
		default:
	}
	return array;
}



	
	

